package com.pengtu.dao.detect;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pengtu.dao.Page;
import com.pengtu.dao.hibernate.HibernateDao;
import com.pengtu.entity.detect.SpotReport;

@Repository
public class SpotreportDao extends HibernateDao<SpotReport, Serializable> {
	
	public boolean batch(String batch,String signid, String oldBatch,
			String oldSignid, String projectId) {
		if(signid != null){
			if (oldBatch.equals(batch) && oldSignid.equals(signid)) {
				return true;
			}
		}
		if(signid == null || signid.equals("")){
			if (oldBatch.equals(batch)) {
				return true;
			}
		}
		String hql = " from SpotReport where 1 = 1 and delFlag = 0";
		
		if (batch != null && !batch.equals("")) {
			hql += " and batch = '" + batch + "'";
		}

		if (signid != null
				&& !signid.equals("")) {
			hql += " and signid = '" + signid + "'";
		}

		if (projectId != null && !projectId.equals("")) {
			hql += " and projectInfo.id = '" + projectId + "'";
		}

		List<SpotReport> list = find(hql);
		if(list.size()>0){
			return false;
		}else return true;
	}

	public Page<SpotReport> getSpotreportListall(Page<SpotReport> page, String startTime, String endTime, String batch, String projectName) {
		
		
		String hql = "from SpotReport a where  not" +
				" exists(from SpotReport b where b.projectInfo.id = a.projectInfo.id and b.createDate > a.createDate and b.delFlag = 0" +
				") and a.delFlag = 0 ";
		
		if(StringUtils.isNotEmpty(startTime)){
			hql += " and a.reportdate >= to_date('"+startTime+"','yyyy-MM-dd')";
		}
		
		if(StringUtils.isNotEmpty(endTime)){
			hql += " and a.reportdate < to_date('"+endTime+"','yyyy-MM-dd')";
		}
		
		if(StringUtils.isNotEmpty(batch)){
			hql += " and a.batch = '" + batch + "'";
		}

		if(StringUtils.isNotEmpty(projectName)){
			hql += " and a.projectInfo.projName like '%" + projectName + "%'";
		}
		return findPage(page, hql);
	}
}
